<?php
namespace app\helpers;

use yii\helpers\Json;

class Logger
{
    /**
     * 写日志
     * @param unknown $log
     * @param string $filePrefix: 文件前缀
     */
    public static function write($log, $filePrefix = '')
    {
        $path = dirname(__DIR__) . '/runtime/logs';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }

        $time = date("Ymd H:i:s");
        $date = date("Ymd");
        $filename = "$path/{$filePrefix}_{$date}.log";
        if (!is_string($log)) {
            $log = Json::encode($log);
        }
        return file_put_contents($filename, "===={$time}==={$log}".PHP_EOL, FILE_APPEND);
    }
}
